package com.ls.task.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import com.ls.fw.commons.log.exception.ExceptionsHelper;
import com.ls.fw.data.rdb.impl.mybatis.mapper.entity.Example;
import com.ls.task.biz.SysTaskBiz;
import com.ls.task.dal.model.SysTask;
import com.ls.task.dal.service.SysTaskService;
import com.ls.task.utils.TaskUtils;

@Service("sysTaskBiz")
public class SysTaskBizImpl implements SysTaskBiz {

	@Resource
	private SysTaskService sysTaskService;
	@Autowired
	private SchedulerFactoryBean  quertzManager;
	
	@Override
	public void save(SysTask model){
		this.sysTaskService.persist(model);
		model = sysTaskService.find(model.getId());
		loadTask(model);
	}
	@Override
	public void loadTask(SysTask model) {
		JobKey key = TaskUtils.jobKey(model);
		if(model.getValidClass()==Boolean.FALSE
		   || model.getDeleteClass()==Boolean.TRUE
		   ){
			try {
				if(quertzManager.getScheduler().checkExists(key)){
					quertzManager.getScheduler().deleteJob(key);
				}
			} catch (SchedulerException e) {
				throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
			}
		}else{
			try {
				if(!quertzManager.getScheduler().checkExists(key)){
					JobDetail jobDetail = TaskUtils.loadTask(model);
					quertzManager.getScheduler().addJob(jobDetail, false);
				}else{
					JobDetail jobDetail = TaskUtils.loadTask(model);
					quertzManager.getScheduler().addJob(jobDetail, true);
				}
			} catch (SchedulerException e) {
				throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
			}
		}
	}
	private void deleteTask(Long id) {
		try {
			SysTask model = sysTaskService.find(id);
			JobKey key = TaskUtils.jobKey(model);
			if(quertzManager.getScheduler().checkExists(key)){
				quertzManager.getScheduler().deleteJob(key);
			}
		} catch (SchedulerException e) {
			throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
		}
	}

	@Override
	public void persist(SysTask forModel) {
		save(forModel);
	}

	@Override
	public void update(SysTask forModel) {
		save(forModel);
	}

	@Override
	public void remove(String[] ids) {
		if(ids !=null){
			for (String id : ids) {
				int i = sysTaskService.remove(new String[]{id+""});
				if(i>0){
					deleteTask(Long.parseLong(id));
				}
			}
		}
	}

	@Override
	public void delete(String[] ids) {
		if(ids !=null){
			try {
				for (String idstr : ids) {
					long id = Long.parseLong(idstr);
					SysTask model = sysTaskService.find(id);
					int i = sysTaskService.delete(id);
					if(i > 0){
						JobKey key = TaskUtils.jobKey(model);
						if(quertzManager.getScheduler().checkExists(key)){
							quertzManager.getScheduler().deleteJob(key);
						}
					}
				}
			} catch (SchedulerException e) {
				throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
			}
		}
	}
	
	/**
	 * 加载任务
	 */
	@Override
	public void loadTask(){
		//DOTO 特别情况：注释掉下面的语句导致jdbc链接卡住
		System.out.println("sysTaskService："+sysTaskService);
		Example record = new Example(SysTask.class);
		record.createCriteria().andEqualTo(SysTask.Columns.deleteClass.property(), Boolean.FALSE)
		.andEqualTo(SysTask.Columns.validClass.property(),Boolean.TRUE);
		List<SysTask> list = sysTaskService.selectByExample(record );
		if(list != null && list.size()>0){
			for (SysTask sysTask : list) {
				loadTask(sysTask);
			}
		}
	}
	 
}
